﻿namespace System.Workflow.ComponentModel.Compiler
{
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Reflection;

    [Serializable]
    public sealed class WorkflowCompilerParameters : CompilerParameters
    {
        private bool checkTypes;
        private string compilerOptions;
        private bool compileWithNoCode;
        private bool generateCCU;
        private string languageToUse;
        private StringCollection libraryPaths;
        private Assembly localAssembly;
        private IServiceProvider serviceProvider;
        private IList<CodeCompileUnit> userCodeCCUs;

        public WorkflowCompilerParameters()
        {
            this.languageToUse = "CSharp";
        }

        public WorkflowCompilerParameters(string[] assemblyNames) : base(assemblyNames)
        {
            this.languageToUse = "CSharp";
        }

        public WorkflowCompilerParameters(string[] assemblyNames, string outputName) : base(assemblyNames, outputName)
        {
            this.languageToUse = "CSharp";
        }

        public WorkflowCompilerParameters(string[] assemblyNames, string outputName, bool includeDebugInformation) : base(assemblyNames, outputName, includeDebugInformation)
        {
            this.languageToUse = "CSharp";
        }

        internal bool CheckTypes
        {
            get
            {
                return this.checkTypes;
            }
        }

        public string CompilerOptions
        {
            get
            {
                return this.compilerOptions;
            }
            set
            {
                this.compilerOptions = value;
                base.CompilerOptions = this.compilerOptions;
                if ((value != null) && value.Contains("/nocode"))
                {
                    this.compileWithNoCode = true;
                    base.CompilerOptions = base.CompilerOptions.Replace(" /nocode", "");
                    base.CompilerOptions = base.CompilerOptions.Replace("/nocode", "");
                }
                else
                {
                    this.compileWithNoCode = false;
                }
                if ((value != null) && value.Contains("/checktypes"))
                {
                    this.checkTypes = true;
                    base.CompilerOptions = base.CompilerOptions.Replace(" /checktypes", "");
                    base.CompilerOptions = base.CompilerOptions.Replace("/checktypes", "");
                }
                else
                {
                    this.checkTypes = false;
                }
            }
        }

        internal bool CompileWithNoCode
        {
            get
            {
                return this.compileWithNoCode;
            }
        }

        public bool GenerateCodeCompileUnitOnly
        {
            get
            {
                return this.generateCCU;
            }
            set
            {
                this.generateCCU = value;
            }
        }

        public string LanguageToUse
        {
            get
            {
                return this.languageToUse;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentNullException("value");
                }
                if ((string.Compare(value, SupportedLanguages.CSharp.ToString(), StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(value, SupportedLanguages.VB.ToString(), StringComparison.OrdinalIgnoreCase) != 0))
                {
                    throw new NotSupportedException(SR.GetString("Error_LanguageNeedsToBeVBCSharp", new object[] { value }));
                }
                this.languageToUse = value;
            }
        }

        public StringCollection LibraryPaths
        {
            get
            {
                if (this.libraryPaths == null)
                {
                    this.libraryPaths = new StringCollection();
                }
                return this.libraryPaths;
            }
        }

        internal Assembly LocalAssembly
        {
            get
            {
                return this.localAssembly;
            }
            set
            {
                this.localAssembly = value;
            }
        }

        internal IServiceProvider ServiceProvider
        {
            get
            {
                return this.serviceProvider;
            }
            set
            {
                this.serviceProvider = value;
            }
        }

        public IList<CodeCompileUnit> UserCodeCompileUnits
        {
            get
            {
                if (this.userCodeCCUs == null)
                {
                    this.userCodeCCUs = new List<CodeCompileUnit>();
                }
                return this.userCodeCCUs;
            }
        }
    }
}

